//
//  VerifyCourierByExpressNumberViewController.m
//  atXiaoGeBClient
//
//  Created by Linyu Niu on 2018/7/2.
//  Copyright © 2018 Linyu Niu. All rights reserved.
//

#import "VerifyCourierByExpressNumberViewController.h"
#import "DeliverDetailViewController.h"

@interface VerifyCourierByExpressNumberViewController ()<QuickQRCodeScanResultHandler,UITextFieldDelegate>
{
    UITextField * numberField;
    NSString * expressNumber;
}
@end

@implementation VerifyCourierByExpressNumberViewController
@synthesize courierID,isRegister;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromRGB(0xEEEEEE);
    __weak VerifyCourierByExpressNumberViewController * weakSelf = self;
    UIView * navView = [[CTMediator sharedInstance] navgationHeadViewInTargetView:self.view params:[NSDictionary dictionaryWithObjectsAndKeys:@"快递身份验证",@"title", nil] leftAction:^(UIButton * sender){
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } rightAction:nil];
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(0,navView.frame.size.height + navView.frame.origin.y + 30,self.view.frame.size.width,30);
    label.text = @"请输入七天内本人派送过的快递单号";
    
    label.font = [UIFont systemFontOfSize:14];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
    [self.view addSubview:label];
    
    UIView * view = [[UIView alloc] init];
    view.frame = CGRectMake(0,label.frame.size.height + label.frame.origin.y + 10,self.view.frame.size.width,50);
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    numberField = [[UITextField alloc]initWithFrame:CGRectMake(5, 5, view.frame.size.width - 60, view.frame.size.height - 10)];
    numberField.delegate = self;
    [view addSubview:numberField];
    
    
    UIButton * scanbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [scanbutton setImage:[UIImage imageNamed:@"qr_icon"] forState:UIControlStateNormal];
    scanbutton.frame = CGRectMake(view.frame.size.width - 55, 0, 55, view.frame.size.height);
    [scanbutton addTarget:self action:@selector(scan) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:scanbutton];
    
    
    UIButton * nextButton = [[CTMediator sharedInstance] commonOrangeButton:CGRectMake(30, view.frame.size.height + view.frame.origin.y + 30, self.view.frame.size.width - 60, 40) title:@"确认提交" delegate:self action:@selector(submit)];
    [self.view addSubview:nextButton];
}

-(void)submit
{
    [self.view endEditing:YES];
    if(expressNumber && expressNumber.length != 0){
        
        DeliverDetailViewController * controller = [[DeliverDetailViewController alloc]init];
        controller.isRegister = isRegister;
        controller.expressNumber = expressNumber;
        controller.courierID = courierID;
        [self.navigationController pushViewController:controller animated:YES];
    }
}


-(void)scan{
    [self.view endEditing:YES];
    QuickBarCodeScanController *scanVC = [QuickBarCodeScanController new];
    
    scanVC.resultHandler = self;
    [self.navigationController pushViewController:scanVC animated:YES];
}

#pragma mark - UITextField
-(void)textFieldDidEndEditing:(UITextField *)textField{
    expressNumber = textField.text;
}
#pragma mark - QRScan
- (BOOL)handleResult:(NSString *)text withQRCodeScanController:(QuickQRCodeScanController *)scanVC{
    
    expressNumber = text;
    [self submit];
    return YES;
}
@end
